package app.action;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.DefaultEditorKit;

import app.ui.MakeDataFileUi;

public class TextContentAction extends MouseAdapter implements DocumentListener {
	private MakeDataFileUi mPanel;
	private JPopupMenu jPopupMenu;
	private JMenuItem jMenuItem;
	private String[] textContents;

	public TextContentAction(MakeDataFileUi mPanel) {
		this.mPanel = mPanel;
		this.jPopupMenu = new JPopupMenu("My paste action");
		this.jMenuItem = jPopupMenu.add("Paste");
		jMenuItem.addActionListener(new DefaultEditorKit.PasteAction());
	}

	public void insertUpdate(DocumentEvent e) {
		veryfyContent();
	}

	public void removeUpdate(DocumentEvent e) {
		veryfyContent();
	}

	public void changedUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		super.mouseReleased(e);
		if (e.getButton() == 3) {
			// paste action
			jPopupMenu.show(mPanel, e.getX(), e.getY());
		}
	}

	private void veryfyContent() {
		JTextArea jArea = mPanel.getjTextArea();
		String text = jArea.getText();

		if (text.trim().length() > 0) {
			mPanel.getjButtons()[1].setEnabled(true);
		} else {
			mPanel.getjButtons()[1].setEnabled(false);
		}

		this.textContents = text.split("\n");
		int num = textContents.length;
		mPanel.getjTextFields()[2].setText(String.valueOf(num));
	}

	public String[] getTextContents() {
		return textContents;
	}

}
